// my/pages/CourseFavorites/CourseFavorites.js
import {
  BASE_URL
} from '../../../http/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    userId: 1, // 可动态从缓存获取
    pageNum: 1,
    pageSize: 10,
    orderList: [],
    hasMore: true,
    loading: false,
    // baseUrl: 'http://localhost:8080',
    baseUrl:''
    

  },
  changeIndex(e){

    this.setData({
      index:e.target.dataset.index
    })
  },
  getOrderList(isRefresh = false) {
    if (this.data.loading) return;
    if (!this.data.hasMore && !isRefresh) return;

    if (isRefresh) {
      this.setData({
        pageNum: 1,
        orderList: [],
        hasMore: true
      });
    }

    this.setData({ loading: true });

    wx.request({
      url: `${BASE_URL}/uco`,
      method: 'GET',
      data: {
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
        userid: this.data.userId
      },
      success: (res) => {
        console.log(res.data.data);
        const list = res.data?.data?.list || [];
        const hasMore = list.length === this.data.pageSize;

        this.setData({
          orderList: isRefresh ? list : this.data.orderList.concat(list),
          pageNum: this.data.pageNum + 1,
          hasMore
        });
      },
      fail: () => {
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({ loading: false });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  goToDetail(e) {
    
    const courseId = e.currentTarget.dataset.courseid;
    console.log(courseId)
    wx.navigateTo({
      url: `/course/pages/courseDetail/courseDetail?id=${courseId}`
    });
  }
  ,
  onLoad(options) {
    let userInfo=wx.getStorageSync('userInfo');
    this.setData({
      userId:userInfo.id

    })
    this.getOrderList();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getOrderList(true);
    wx.stopPullDownRefresh();

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getOrderList();

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})